package top.yueyazhui.business.service.impl;

import io.seata.core.context.RootContext;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.yueyazhui.business.service.BusinessService;
import top.yueyazhui.common.feign.OrderFeign;
import top.yueyazhui.common.feign.StorageFeign;

@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    OrderFeign orderFeign;
    @Autowired
    StorageFeign storageFeign;

    @Override
    @GlobalTransactional
    public void purchase(String userId, String commodityCode, Integer count, Double money) {
        // 获取全局事务的 ID
        String xid = RootContext.getXID();
        BusinessActionContext businessActionContext = new BusinessActionContext();
        businessActionContext.setXid(xid);
        storageFeign.prepare(businessActionContext, commodityCode, count);
        orderFeign.prepare(businessActionContext, userId, commodityCode, count, money);
    }
}
